package CGW.controller;

import CGW.bean.Dingdan;
import CGW.bean.Page;
import CGW.dao.DingdanDao;
import CGW.service.DingdanService;
import CGW.utils.ZHI;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/dingdan")
public class DingdanServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {


        DingdanService service = new DingdanService(new DingdanDao());
        String its = req.getParameter("action");
        if (its == null){
            Page<Dingdan> pageBean1= service.queryAll(1, 100);
            System.out.println(pageBean1.getDatas());
            req.setAttribute("pageBean1",pageBean1);
            req.getRequestDispatcher("dingdan.jsp").forward(req,resp);
        } else if(its.equals("aaa")){
            String user_id = req.getParameter("user_id");
            String t = req.getParameter("totalprice");
            int table_id = (int)req.getSession().getAttribute("table_id");
            int table_sum = (int)req.getSession().getAttribute("total_sum");
            System.out.println(user_id);
            System.out.println(t);
            System.out.println(table_id);
            System.out.println(table_sum);
            Dingdan dingdan = null;

            dingdan = new Dingdan(table_id,ZHI.zh(user_id),table_sum,Double.parseDouble(t),0);
            boolean ret = service.insert(dingdan);
            if (ret){
                System.out.println(ret);
            }
            resp.sendRedirect("/dingdan");
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    }
}
